package com.ruoyi.web.controller.constructionLike;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.constructionLike.ConstructionCollected;
import com.ruoyi.system.domain.constructionLike.ConstructionLike;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.service.constructionLike.ConstructionCollectedService;
import com.ruoyi.system.service.constructionLike.ConstructionLikeService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 施工攻略点赞 Controller
 *
 * @author ruoyi
 * @date 2021-04-10
 */
@RestController
@RequestMapping("/api/constructionLike/like")
public class ConstructionLikeController extends BaseController {
    @Autowired
    private ConstructionLikeService constructionLikeService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ConstructionCollectedService constructionCollectedService;

    /**
     * 查询施工攻略点赞 列表
     */
    @GetMapping(value = "/list/{constructionStrategyId}")
    public AjaxResult list(@PathVariable("constructionStrategyId") Long constructionStrategyId)
    {
        ConstructionLike constructionLike = new ConstructionLike();
        CustomerLogin loginCustomer = tokenUtils.getLoginCustomer(request);
        Long customerId = loginCustomer.getCustomerId();
        constructionLike.setConstructionStrategyId(Math.toIntExact(constructionStrategyId));
        constructionLike.setUserId(Math.toIntExact(customerId));
        ConstructionLike constructionLike1 = constructionLikeService.selectConstructionLikeList(constructionLike);
        if (constructionLike1 != null && !constructionLike1.equals("")){
            //点过赞
            return AjaxResult.success(constructionLike1);
        }else{
            //未进行过点赞
            ConstructionLike constructionLike2 = new ConstructionLike();
            constructionLike2.setStatus("0");
            return AjaxResult.success(constructionLike2);
        }
    }

    /**
     * 查询施工攻略收藏列表
     */
    @GetMapping(value = "/constructionList/{constructionStrategyId}")
    public AjaxResult lists(@PathVariable("constructionStrategyId") Long constructionStrategyId)
    {
        ConstructionCollected constructionCollected = new ConstructionCollected();
        CustomerLogin loginCustomer = tokenUtils.getLoginCustomer(request);
        Long customerId = loginCustomer.getCustomerId();
        constructionCollected.setUserId(Math.toIntExact(customerId));
        constructionCollected.setConstructionStrategyId(Math.toIntExact(constructionStrategyId));
        ConstructionCollected constructionCollected1 = constructionCollectedService.selectConstructionCollectedList(constructionCollected);
        if (constructionCollected1 != null && !constructionCollected1.equals("")){
            return AjaxResult.success(constructionCollected1);
        }else {
            ConstructionCollected constructionCollected2 = new ConstructionCollected();
            constructionCollected2.setStatus("0");
            return AjaxResult.success(constructionCollected2);
        }
    }

    /**
     * 新增施工攻略点赞
     */
    @Log(title = "施工攻略点赞 ", businessType = BusinessType.INSERT)
    @PostMapping("addLike")
    public AjaxResult add(@RequestBody ConstructionLike constructionLike)
    {
        CustomerLogin loginCustomer = tokenUtils.getLoginCustomer(request);
        Long customerId = loginCustomer.getCustomerId();
        constructionLike.setUserId(Math.toIntExact(customerId));
        return toAjax(constructionLikeService.insertConstructionLike(constructionLike));
    }

    /**
     * 新增施工攻略收藏
     */
    @Log(title = "施工攻略收藏", businessType = BusinessType.INSERT)
    @PostMapping("addConstruction")
    public AjaxResult add(@RequestBody ConstructionCollected constructionCollected)
    {
        CustomerLogin loginCustomer = tokenUtils.getLoginCustomer(request);
        Long customerId = loginCustomer.getCustomerId();
        constructionCollected.setUserId(Math.toIntExact(customerId));
        return toAjax(constructionCollectedService.insertConstructionCollected(constructionCollected));
    }

    /**
     * 所有收藏
     */
    @PostMapping("/constructionCount")
    public int constructionCount(Integer constructionStrategyId){
        return constructionCollectedService.selectConstructionCollectedCount(constructionStrategyId);
    }

    /**
     * 所有点赞
     */
    @GetMapping("/constructionLikeCount")
    public int constructionLikeCount(){
        return constructionLikeService.selectConstructionLikeCount();
    }
}
